package com.candy.th5;

public class Bank {
    //账户余额
    private int account=100;
    private Object lock_getMoney=new Object();
    private Object lock_ask=new Object();

    //取钱
    public int getMoney(int order) {
        synchronized (lock_getMoney) {
            //判断，当账户余额大于取款金额，则正常取款
            if (account >= order) {
                System.out.println("系统正在处理。。。。。");
                try {
                    //模拟等待时间
                    Thread.sleep((long) ((Math.random() * 2000) + 600));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //取款结束后，账户余额发生改变，当前余额为账户余额减去取款余额
                account -= order;
                //打印取款金额及余额
                System.out.println("尊敬的" + Thread.currentThread().getName() + "用户，您本次的取款金额是"
                        + order + "元，当前账户余额为" + account + "元");
                //取款正常，返回取款金额
                return order;
            } else {
                //若取款金额大于账户余额，则取款失败，提示账户余额不足
                System.out.println("很抱歉，余额不足。。。。");
                //不能正常取款，取款金额为0
                return 0;
            }
        }
    }
    //咨询业务
    public void ask() {
        synchronized (lock_ask) {
            System.out.println(Thread.currentThread().getName() + "正在咨询业务");
            try {
                Thread.sleep((long) ((Math.random() * 2000) + 600));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "咨询业务完毕");
        }
    }
}
